package com.dofuntech.dataplatform.protocal;

import com.dofuntech.dataplatform.util.ByteEncoder;

import org.apache.mina.core.buffer.IoBuffer;

import java.io.IOException;

/**
 * ʧ����Ϣ��
 * <p>
 * @author DavidChen 
 */
public class FailBody extends MessageBody<FailBody> {
	
	private ErrorCode errorCode;
	
	FailBody() {
		
	}
	
	public FailBody(ErrorCode errorCode) {
		this.errorCode = errorCode;
	}

	@Override
	public Command command() {
		return Command.FAIL;
	}
	
	@Override
	public int getContentLength() {
		return 1;
	}

	@Override
	public void writeTo(IoBuffer encoder) throws IOException {
		encoder.put(errorCode.getCode());
	}

	@Override
	public FailBody decode(byte[] buffer, int offset, int byteCount) {
		this.errorCode = ErrorCode.getByCode(buffer[offset]);
		return this;
	}

	public ErrorCode getErrorCode() {
		return errorCode;
	}
	
	@Override
	public String toString() {
		return "--����ʧ��--,��Ϣ����" + getContentLength() + ",�����룺" + errorCode.getDesc();
	}
}
